home *** CD-ROM | disk | FTP | other *** search
/ The Arsenal Files 4 / The Arsenal Files 4 (Arsenal Computer).ISO / ham / sattrk31.tgz / sattrack-3.1.tar / SatTrack / src / sattrack / satctrl.c < prev    next >
C/C++ Source or Header  |  1995-03-16  |  17KB  |  496 lines

  1. /******************************************************************************/
  2. /*                                                                            */
  3. /*  Title       : satctrl.c                                                   */
  4. /*  Author      : Manfred Bester                                              */
  5. /*  Date        : 23Jan94                                                     */
  6. /*  Last change : 15Mar95                                                     */
  7. /*                                                                            */
  8. /*  Synopsis    : Routines for controlling satellite communications hardware  */
  9. /*                (antenna interface, radio).                                 */
  10. /*                                                                            */
  11. /*                                                                            */
  12. /*  SatTrack is Copyright (c) 1992, 1993, 1994, 1995 by Manfred Bester.       */
  13. /*  All Rights Reserved.                                                      */
  14. /*                                                                            */
  15. /*  Permission to use, copy, and distribute SatTrack and its documentation    */
  16. /*  in its entirety for educational, research and non-profit purposes,        */
  17. /*  without fee, and without a written agreement is hereby granted, provided  */
  18. /*  that the above copyright notice and the following three paragraphs appear */
  19. /*  in all copies. SatTrack may be modified for personal purposes, but        */
  20. /*  modified versions may NOT be distributed without prior consent of the     */
  21. /*  author.                                                                   */
  22. /*                                                                            */
  23. /*  Permission to incorporate this software into commercial products may be   */
  24. /*  obtained from the author, Dr. Manfred Bester, 1636 M. L. King Jr. Way,    */
  25. /*  Berkeley, CA 94709, USA. Note that distributing SatTrack 'bundled' in     */
  26. /*  with ANY product is considered to be a 'commercial purpose'.              */
  27. /*                                                                            */
  28. /*  IN NO EVENT SHALL THE AUTHOR BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, */
  29. /*  SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OF   */
  30. /*  THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE AUTHOR HAS BEEN ADVISED  */
  31. /*  OF THE POSSIBILITY OF SUCH DAMAGE.                                        */
  32. /*                                                                            */
  33. /*  THE AUTHOR SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT      */
  34. /*  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A   */
  35. /*  PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS"      */
  36. /*  BASIS, AND THE AUTHOR HAS NO OBLIGATIONS TO PROVIDE MAINTENANCE, SUPPORT, */
  37. /*  UPDATES, ENHANCEMENTS, OR MODIFICATIONS.                                  */
  38. /*                                                                            */
  39. /******************************************************************************/
  40.  
  41. #include <stdio.h>
  42. #include <string.h>
  43. #include <math.h>
  44.  
  45. #ifndef STDLIB
  46. #include <stdlib.h>
  47. #endif
  48.  
  49. #include "satglobalsx.h"
  50. #include "sattrack.h"
  51.  
  52. /******************************************************************************/
  53. /*                                                                            */
  54. /* control flags for the ISI telescope control system                         */
  55. /*                                                                            */
  56. /******************************************************************************/
  57.  
  58. #ifdef ISI
  59. #undef  ANTENNATYPE
  60. #define ANTENNATYPE   ISITELESCOPE
  61. #undef  RADIOTYPE
  62. #define RADIOTYPE     NONE
  63. #endif
  64.  
  65. /******************************************************************************/
  66. /*                                                                            */
  67. /* trackAntenna: controls movement of the satellite antenna                   */
  68. /*                                                                            */
  69. /* THIS FUNCTION NEEDS TO BE ADAPTED TO THE PARTICULAR ANTENNA CONTROLLER     */
  70. /* USED. SPECIFY THE ANTENNA CONTROLLER TYPE IN 'sattrack.h'.                 */
  71. /*                                                                            */
  72. /******************************************************************************/
  73.  
  74. void trackAntenna()
  75.  
  76. {
  77.     double sendEle, sendEleRate, downFreq, upFreq;
  78.     int    error;
  79.     char   sendDownMode[20], sendUpMode[20];
  80.  
  81.     /* the next few lines are required here also, because some ground station */
  82.     /* equipment like the TrakBox require the antenna and radio information   */
  83.     /* to be sent out in one single command string                            */
  84.  
  85.     if (trackObject == SAT)
  86.     {
  87.         downFreq = (downlinkFreq + freqOffset + downlinkDopp) * CHZMHZ;
  88.         upFreq   = (uplinkFreq + freqOffset*xponderSign + uplinkDopp) * CHZMHZ;
  89.  
  90.         sprintf(sendDownMode,"%s",downlinkMode);
  91.         sprintf(sendUpMode,"%s",uplinkMode);
  92.     }
  93.  
  94.     else
  95.     {
  96.         downFreq = (downlinkFreq + freqOffset) * CHZMHZ;
  97.         upFreq   = (uplinkFreq + freqOffset*xponderSign) * CHZMHZ;
  98.  
  99.         sprintf(sendDownMode,"NONE");
  100.         sprintf(sendUpMode,"NONE");
  101.     }
  102.  
  103.     trackAziRate = trackAzimuth   - lastTrackAzi;
  104.     trackEleRate = trackElevation - lastTrackEle;
  105.  
  106.     /* the next two lines are needed to make tracking smooth */
  107.     /* for pass across north                                 */
  108.  
  109.     if (fabs(trackAzimuth - lastTrackAzi) > PI)
  110.         trackAziRate += (trackAzimuth < lastTrackAzi) ? TWOPI : -TWOPI;
  111.  
  112.     /* the next four lines are needed to limit the tracking speed */
  113.     /* of the antenna in azimuth and elevation                    */
  114.  
  115.     if (fabs(trackAziRate) > MAXAZIRATE)
  116.         trackAziRate = (trackAziRate > 0.0) ? MAXAZIRATE : -MAXAZIRATE;
  117.  
  118.     if (fabs(trackEleRate) > MAXELERATE)
  119.         trackEleRate = (trackEleRate > 0.0) ? MAXELERATE : -MAXELERATE;
  120.  
  121.     lastTrackAzi = trackAzimuth;
  122.     lastTrackEle = trackElevation;
  123.  
  124.     /* the next two lines are needed to make tracking smooth near */
  125.     /* the horizon in the presence of atmospheric refraction      */
  126.  
  127.     sendEle      = (trackElevation > HALFDEG) ? trackElevation : ZERO;
  128.     sendEleRate  = (trackElevation > HALFDEG) ? trackEleRate   : ZERO;
  129.  
  130.     if (!trackCtrlFlag)
  131.         return;
  132.  
  133.     switch(ANTENNATYPE) 
  134.     {
  135.         case NONE:
  136.             break;
  137.  
  138.         case GENERIC:
  139.             if (antennaFile)
  140.             {
  141.                 fprintf(antennaFile,
  142.                     "satAntenna: %8ld %8.3f %7.3f %7.3f %7.3f\n",
  143.                     curYearSec,trackAzimuth*CRD,sendEle*CRD,
  144.                     trackAziRate*CRD,sendEleRate*CRD);
  145.                 fflush(antennaFile);
  146.             }
  147.  
  148.             break;
  149.  
  150.         case KANSASCITY:
  151.             if (antennaFile)
  152.             {
  153.                 /* THE NEXT LINE NEEDS TO BE CONFIGURED BY THE USER */
  154.  
  155.                 fprintf(antennaFile,"%8.3f %7.3f %7.3f %7.3f\n",
  156.                     trackAzimuth*CRD,sendEle*CRD,
  157.                     trackAziRate*CRD,sendEleRate*CRD);
  158.                 fflush(antennaFile);
  159.             }
  160.  
  161.             break;
  162.  
  163.         /************************************************************/
  164.         /* JR1EDE, 04Jan95                                          */
  165.         /*                                                          */
  166.         /* JA6FTL TrakBox "HostMode"                                */
  167.         /*        controls both radio and antenna by a serial line  */
  168.         /*        and sends the mode and frequency infos to it's    */
  169.         /*        attached radio by another serial line.            */
  170.         /*        The baud rate is user configurable up to 9600.    */
  171.         /*                                                          */
  172.         /*        Newline: NL -> CR or NL -> NL                     */
  173.         /*                                                          */
  174.         /************************************************************/
  175.  
  176.         case TRAKBOX:
  177.             if (antennaFile)
  178.             {
  179.                 fprintf(antennaFile,"AZ%03.0f EL%02.0f ",
  180.                     trackAzimuth*CRD,sendEle*CRD);
  181.  
  182.                 /* send downlink only if mode is specified correctly */
  183.  
  184.                 if (strlen(sendDownMode) && strncmp("NONE",sendDownMode,4))
  185.                 {
  186.                     fprintf(antennaFile,"MD%s ",downlinkMode);
  187.                     fprintf(antennaFile,"FD%011.0f ",downFreq*1e8);
  188.                 }
  189.  
  190.                 /* send uplink only if mode is specified correctly */
  191.  
  192.                 if (strlen(sendUpMode) && strncmp("NONE",sendUpMode,4))
  193.                 {
  194.                     fprintf(antennaFile,"MU%s ",uplinkMode);
  195.                     fprintf(antennaFile,"FU%011.0f",upFreq*1e8);
  196.                 }
  197.  
  198.                 fprintf(antennaFile,"\n");
  199.                 fflush(antennaFile);
  200.             }
  201.  
  202.             break;
  203.  
  204.         case ISITELESCOPE:
  205.             error = controlISI();
  206.  
  207.             if (error)
  208.             {
  209.                 trackingFlag  = ERROR;
  210.                 trackDispFlag = TRUE;
  211.             }
  212.  
  213.             break;
  214.  
  215.         default:
  216.             break;
  217.     }
  218.  
  219.     return;
  220. }
  221.  
  222. /******************************************************************************/
  223. /*                                                                            */
  224. /* trackRadio: control satellite radio(s) (frequency, Doppler shift, etc.)    */
  225. /*                                                                            */
  226. /* THIS FUNCTION NEEDS TO BE ADAPTED TO THE PARTICULAR RADIO USED.            */
  227. /* SPECIFY THE RADIO TYPE IN 'sattrack.h'.                                    */
  228. /*                                                                            */
  229. /******************************************************************************/
  230.  
  231. void trackRadio()
  232.  
  233. {
  234.     double downFreq, upFreq;
  235.     char   sendDownMode[20], sendUpMode[20];
  236.  
  237.     if (trackObject == SAT)
  238.     {
  239.         downFreq = (downlinkFreq + freqOffset + downlinkDopp) * CHZMHZ;
  240.         upFreq   = (uplinkFreq + freqOffset*xponderSign + uplinkDopp) * CHZMHZ;
  241.  
  242.         sprintf(sendDownMode,"%s",downlinkMode);
  243.         sprintf(sendUpMode,"%s",uplinkMode);
  244.     }
  245.  
  246.     else
  247.     {
  248.         downFreq = (downlinkFreq + freqOffset) * CHZMHZ;
  249.         upFreq   = (uplinkFreq + freqOffset*xponderSign) * CHZMHZ;
  250.  
  251.         sprintf(sendDownMode,"NONE");
  252.         sprintf(sendUpMode,"NONE");
  253.     }
  254.  
  255.     if (!trackCtrlFlag)
  256.         return;
  257.  
  258.     switch(RADIOTYPE)
  259.     {
  260.         case NONE:
  261.             break;
  262.  
  263.         case GENERIC:
  264.             if (radioFileA)
  265.             {
  266.                 fprintf(radioFileA,"satRadioA: %8ld  %s  %.6f  %s  %.6f\n",
  267.                     curYearSec,downlinkMode,downFreq,uplinkMode,upFreq);
  268.                 fflush(radioFileA);
  269.             }
  270.  
  271.             break;
  272.  
  273.         case FT736:
  274.             if (radioFileA)
  275.             {
  276.                 /* THE NEXT LINE NEEDS TO BE CONFIGURED BY THE USER */
  277.  
  278.                 fprintf(radioFileA,"%.6f %.6f\n",downFreq,upFreq);
  279.                 fflush(radioFileA);
  280.             }
  281.  
  282.             break;
  283.  
  284.         case IC970:
  285.             if (radioFileA)
  286.             {
  287.                 /* THE NEXT LINE NEEDS TO BE CONFIGURED BY THE USER */
  288.  
  289.                 fprintf(radioFileA,"%.6f %.6f\n",downFreq,upFreq);
  290.                 fflush(radioFileA);
  291.             }
  292.  
  293.             break;
  294.  
  295.         /*************************************/
  296.         /* JR1EDE, 04Jan95                   */
  297.         /* TS-790 serial control information */
  298.         /*************************************/
  299.         
  300.         case TS790:
  301.             if (radioFileA)
  302.             {
  303.                 /* send downlink only if mode is specified correctly */
  304.  
  305.                 if (strlen(sendDownMode) && strncmp("NONE",sendDownMode,4))
  306.                 {
  307.                     fprintf(radioFileA,"MD%s ",downlinkMode);
  308.                     fprintf(radioFileA,"FD%011.0f ",downFreq*1e8);
  309.                 }
  310.  
  311.                 /* send uplink only if mode is specified correctly */
  312.  
  313.                 if (strlen(sendUpMode) && strncmp("NONE",sendUpMode,4))
  314.                 {
  315.                     fprintf(radioFileA,"MU%s ",uplinkMode);
  316.                     fprintf(radioFileA,"FU%011.0f",upFreq*1e8);
  317.                 }
  318.  
  319.                 fprintf(radioFileA,"\n");
  320.                 fflush(radioFileA);
  321.             }
  322.  
  323.             break;
  324.  
  325.         case DUAL:
  326.             if (radioFileA) /* downlink radio */
  327.             {
  328.                 /* THE NEXT LINE NEEDS TO BE CONFIGURED BY THE USER */
  329.  
  330.                 fprintf(radioFileA,"satRadioA: %8ld %s %.6f\n",
  331.                     curYearSec,downlinkMode,downFreq);
  332.                 fflush(radioFileA);
  333.             }
  334.  
  335.             if (radioFileB) /* uplink radio */
  336.             {
  337.                 /* THE NEXT LINE NEEDS TO BE CONFIGURED BY THE USER */
  338.  
  339.                 fprintf(radioFileB,"satRadioB: %8ld %s %.6f\n",
  340.                     curYearSec,uplinkMode,upFreq);
  341.                 fflush(radioFileB);
  342.             }
  343.  
  344.             break;
  345.  
  346.         default:
  347.             break;
  348.     }
  349.  
  350.     return;
  351. }
  352.  
  353. /******************************************************************************/
  354. /*                                                                            */
  355. /* trackingControl: controls communications hardware                          */
  356. /*                                                                            */
  357. /*               'lastTrackYearSec' is used to avoid duplicate messages when  */
  358. /*               switching between SINGLE and MULTISAT live displays          */
  359. /*                                                                            */
  360. /*               'trackCtrlFlag' is used to inhibit the first message after   */
  361. /*               changing the object to avoid big azimuth and elevation rates */
  362. /*                                                                            */
  363. /******************************************************************************/
  364.  
  365. void trackingControl()
  366.  
  367. {
  368.     double trackLimit;
  369.  
  370.     switch(trackObject)
  371.     {
  372.         case SUN:
  373.             trackAzimuth   = sunAzimuth;
  374.             trackElevation = sunElevation;
  375.             break;
  376.  
  377.         case MOON:
  378.             trackAzimuth   = moonAzimuth;
  379.             trackElevation = moonElevation;
  380.             break;
  381.  
  382.         case SAT:
  383.             trackAzimuth   = satAzimuth;
  384.             trackElevation = satElevation;
  385.             break;
  386.  
  387.         default:
  388.             break;
  389.     }
  390.  
  391.     trackLimit = (ANTENNATYPE  == ISITELESCOPE) ?         -PI : TRACKLIMIT1;
  392.     trackLimit = (trackingFlag == AUTOTRK)      ? TRACKLIMIT2 : trackLimit;
  393.  
  394.     if (!preOrbitFlag && trackingFlag > 0 && trackElevation > trackLimit)
  395.     {
  396.         if (curYearSec != lastTrackYearSec)
  397.         {
  398.             trackAntenna();
  399.             trackRadio();
  400.  
  401.             lastTrackYearSec = curYearSec; 
  402.             trackCtrlFlag    = TRUE;
  403.         }
  404.     }
  405.  
  406.     return;
  407. }
  408.  
  409. /******************************************************************************/
  410. /*                                                                            */
  411. /* initSatIO: checks if I/O devices are accessible (owned by user)            */
  412. /*                                                                            */
  413. /******************************************************************************/
  414.  
  415. int initSatIO()
  416.  
  417. {
  418.     int  error;
  419.     char msgStr[80];
  420.  
  421.     if (!antennaFile && ANTENNATYPE != NONE)
  422.     {
  423.         sprintf(msgStr,"Trying to open '%s' ...",antennaIO);
  424.         dispMessage(msgStr);
  425.  
  426.         antennaFile = fopen(antennaIO, "a");
  427.     }
  428.  
  429.     if (!radioFileA && RADIOTYPE != NONE)
  430.     {
  431.         sprintf(msgStr,"Trying to open '%s' ...",radioIOA);
  432.         dispMessage(msgStr);
  433.  
  434.         radioFileA = fopen(radioIOA, "a");
  435.     }
  436.  
  437.     if (!radioFileB && RADIOTYPE == DUAL)
  438.     {
  439.         sprintf(msgStr,"Trying to open '%s' ...",radioIOB);
  440.         dispMessage(msgStr);
  441.  
  442.         radioFileB = fopen(radioIOB, "a");
  443.     }
  444.  
  445.     error = (!antennaFile || 
  446.              (!radioFileA && ANTENNATYPE != TRAKBOX) || 
  447.              (!radioFileB && RADIOTYPE == DUAL)) ? TRUE : FALSE;
  448.  
  449.     if (!error)
  450.     {
  451.         *msgStr = '\0';
  452.         dispMessage(msgStr);
  453.     }
  454.  
  455.     return(error);
  456. }
  457.  
  458. /******************************************************************************/
  459. /*                                                                            */
  460. /* closeSatIO: closes I/O devices                                             */
  461. /*                                                                            */
  462. /******************************************************************************/
  463.  
  464. int closeSatIO()
  465.  
  466. {
  467.     int error;
  468.  
  469.     if (antennaFile)
  470.     {
  471.         fclose(antennaFile);
  472.         antennaFile = NULL;
  473.     }
  474.  
  475.     if (radioFileA)
  476.     {
  477.         fclose(radioFileA);
  478.         radioFileA = NULL;
  479.     }
  480.  
  481.     if (radioFileB)
  482.     {
  483.         fclose(radioFileB);
  484.         radioFileB = NULL;
  485.     }
  486.  
  487.     error = FALSE;
  488.     return(error);
  489. }
  490.  
  491. /******************************************************************************/
  492. /*                                                                            */
  493. /* End of function block satctrl.c                                            */
  494. /*                                                                            */
  495. /******************************************************************************/
  496.